//
//  collectionViewController.m
//  KeepFit
//
//  Created by 杨旭 on 15/11/7.
//  Copyright © 2015年 Fuzzie Liu. All rights reserved.
//

#import "collectionViewController.h"
#import "MyModal.h"
#import "CollectionTableViewCell.h"
#import "FinalViewController.h"

@interface collectionViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) FMDatabase *db;//数据库对象
@property (nonatomic, strong) NSMutableArray *dateAllArr;//所有数据库对象
@property (nonatomic, strong) NSString *indexCode;//标识点击的 cell 上的 code
@end

@implementation collectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *backView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    backView.image = [UIImage imageNamed:@"beijingtu.jpeg"];
    UIBlurEffect *blueEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blueEffect];
    effectView.frame = self.view.bounds;
    [backView addSubview:effectView];
    
    self.title = @"我的收藏";
    self.navigationController.navigationBar.translucent = NO;//设置导航栏不透明
    self.dateAllArr = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 49) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.backgroundView = backView;
    
    //拼接一个数据库路径
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"KeepFit.sqlite"];
    NSLog(@"%@",path);
    //创建数据库实例对象
    self.db = [FMDatabase databaseWithPath:path];
    //打开数据库
    if (![self.db open]) {
        NSLog(@"数据库打开失败");
    }else{
        NSLog(@"打开数据库成功");
        self.dateAllArr = [self seleteAllDate];
        [self.tableView reloadData];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dateAllArr.count == 0) {
        return 0;
    }else{
        return self.dateAllArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyModal *model = [self.dateAllArr objectAtIndex:indexPath.row];
    CollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[CollectionTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:model.thumb_image_url]];
    cell.myTextLabel.text = model.name;
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    return cell;
}

//查找所有收藏的数据
- (NSMutableArray *)seleteAllDate{
    FMResultSet *rs = [self.db executeQuery:@"select * from t_foodDetail;"];
    NSMutableArray *tempMutableArray = [NSMutableArray array];
    while (rs.next) {
        MyModal *model = [[MyModal alloc] init];
        model.code = [rs stringForColumn:@"code"];
        model.name = [rs stringForColumn:@"name"];
        model.thumb_image_url = [rs stringForColumn:@"thumb_image_url"];
        [tempMutableArray addObject:model];
        NSLog(@"====%@,%@",model.name,model.thumb_image_url);
    }
    return tempMutableArray;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.bounds.size.width / 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     MyModal *model = [self.dateAllArr objectAtIndex:indexPath.row];
    FinalViewController *finalVC = [[FinalViewController alloc] init];
    finalVC.foodCode = model.code;
    [self.navigationController pushViewController:finalVC animated:YES];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        MyModal *model = [self.dateAllArr objectAtIndex:indexPath.row];
        [self.db executeUpdate:@"delete from t_foodDetail where code = ?;",model.code];
        [self.dateAllArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;//设置 navigationBar隐藏
    
    self.dateAllArr = [self seleteAllDate];
    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
